package com.orange.studio.dbk.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import com.orange.studio.dbk.adapters.ListFavioritesSongAdapter;
import com.orange.studio.dbk.dto.SongDTO;


public class DBKUtils {
	public static void checkUserLiked(List<SongDTO> listSongs,List<Integer> listIds){
		try {
			for (int i = 0; i < listIds.size(); i++) {
				int id=listIds.get(i);
				for (int j = 0; j < listSongs.size(); j++) {
					if(listSongs.get(j).getId()==id){
						listSongs.get(j).setLiked(1);
					}
				}
			}
		} catch (Exception e) {
		}
	}
	public static void setUserLikeSongs(List<SongDTO> listSongs,int id){
		try {
			for (int j = 0; j < listSongs.size(); j++) {
				if(listSongs.get(j).getId()==id){
					listSongs.get(j).setLiked(1);
					break;
				}
			}
		} catch (Exception e) {
		}
	}
	public static void setUserLikedIds(List<Integer> listIds,int id){
		try {
			boolean isReady=false;
			for (int j = 0; j < listIds.size(); j++) {
				if(listIds.get(j)==id){
					isReady=true;
					break;
				}
			}
			if(!isReady){
				listIds.add(id);
			}
		} catch (Exception e) {
		}
	}
	public static void setUserUnLikeSongs(List<SongDTO> listSongs,int id){
		try {
			for (int j = 0; j < listSongs.size(); j++) {
				if(listSongs.get(j).getId()==id){
					listSongs.get(j).setLiked(0);
					break;
				}
			}
		} catch (Exception e) {
		}
	}
	public static void removeUserUnLikeSongs(ListFavioritesSongAdapter mAdapter, int id){
		try {
			for (int j = 0; j < mAdapter.getListData().size(); j++) {
				if(mAdapter.getListData().get(j).getId()==id){
					mAdapter.getListData().remove(j);
					mAdapter.notifyDataSetChanged();
					break;
				}
			}
		} catch (Exception e) {
		}
	}
	public static void setUserUnLikedIds(List<Integer> listIds,int id){
		try {
			boolean isReady=false;
			int index=0;
			for (int j = 0; j < listIds.size(); j++) {
				if(listIds.get(j)==id){
					isReady=true;
					index=j;
					break;
				}
			}
			if(isReady){
				listIds.remove(index);
			}
		} catch (Exception e) {
		}
	}
	public static String getStringFromURL(String url) {
		StringBuilder builder = new StringBuilder();
		HttpClient client = new DefaultHttpClient();
		HttpGet httpGet = new HttpGet(url);						
		try {
			HttpResponse response = client.execute(httpGet);
			StatusLine statusLine = response.getStatusLine();
			
			int statusCode = statusLine.getStatusCode();
			if (statusCode == 200) {
				HttpEntity entity = response.getEntity();
				InputStream content = entity.getContent();				
				BufferedReader reader = new BufferedReader(
						new InputStreamReader(content,"Shift_JIS"));
				String line;
				while ((line = reader.readLine()) != null) {
					builder.append(line);
				}
			} else {
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
		return builder.toString();
	}
}
